package com.elastic.config;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

/**
 * @author panghu
 * @version 1.0.0
 * @createTime 2023年08月27日 18:39:00
 * @Description
 */
@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {

    @Autowired
    private ElasticProperties elasticProperties;

    /**
     * 配置es的客户端
     * @return
     */
    @Override
    @Bean("elasticsearchClient")
    public RestHighLevelClient elasticsearchClient() {
        ClientConfiguration configuration =
                ClientConfiguration.builder()
                        .connectedTo(elasticProperties.getHost())
                        .withBasicAuth(elasticProperties.getUsername(), elasticProperties.getPassword())
                        .build();
        return RestClients.create(configuration).rest();
    }

}
